package hot100.logos31To40;

import hot100.datastruct.TreeNode;

/**
 * @author logos
 * date 2025/7/13 13:00
 * @version v1.0
 * @Package hot100.logos31To40
 */
public class logos37 {

    /**
     * 37. 二叉树的最大深度
     * https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/?envType=study-plan-v2&envId=top-100-liked
     */

    class Solution1 {
        public int maxDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int left = maxDepth(root.left);
            int right = maxDepth(root.right);
            return Math.max(left, right) + 1;
        }
    }

    class Solution2 {
        private int ans;

        public int maxDepth(TreeNode root) {
            dfs(root, 0);
            return ans;
        }

        private void dfs(TreeNode root, int depth) {
            if (root == null) {
                return;
            }
            depth++;
            ans = Math.max(ans, depth);
            dfs(root.left, depth);
            dfs(root.right, depth);
        }
    }
}
